//
//  MeChangeInfoView.m
//  BGM
//
//  Created by liuqin on 2018/11/30.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "MeChangeInfoView.h"
#import "Common.h"
#import "BGMUser.h"

#import "MeChangeTableViewCell.h"
#import <objc/runtime.h>
#import "SVProgressHUD.h"
@interface  MeChangeInfoView()<MeChangeCellDelegate>

@end


@implementation MeChangeInfoView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self != nil) {
        self.userSeleArray = [[NSMutableArray alloc]init];
        
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
        bgView.backgroundColor = [UIColor grayColor];
        bgView.alpha = 0.4;
        [self addSubview:bgView];
        
        self.nameView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW-60, 100)];
        self.nameView.backgroundColor = [UIColor whiteColor];
        self.nameView.center = CGPointMake(SCREENW/2, SCREENH/2);
        self.nameView.layer.cornerRadius = 8;
        self.nameView.layer.masksToBounds = YES;
        [self addSubview:self.nameView];
        
        self.nameText = [[UITextField alloc]initWithFrame:CGRectMake(15, 15, self.nameView.frame.size.width-30, self.nameView.frame.size.height/2)];
        self.nameText.placeholder = @"填写真实姓名能方便你的朋友找到你";
        self.nameText.font = [UIFont systemFontOfSize:17];
        [self.nameView addSubview:self.nameText];
        
        float w = self.nameView.frame.size.width/4; //button W
        UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(w+w, self.nameView.frame.size.height/2, w, self.nameView.frame.size.height/2)];
        [btn1 setTitle:@"取消" forState:UIControlStateNormal];
        [btn1 setTitleColor:bgmgrayColor forState:UIControlStateNormal];
        btn1.tag = 100;
        [self.nameView addSubview:btn1];
        
        UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(w+w+w, self.nameView.frame.size.height/2, w, self.nameView.frame.size.height/2)];
        [btn2 setTitle:@"确定" forState:UIControlStateNormal];
        [btn2 setTitleColor:bgmgrayColor forState:UIControlStateNormal];
        [self.nameView addSubview:btn2];
        btn2.tag = 101;
        [btn1 addTarget:self action:@selector(clickNameBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btn2 addTarget:self action:@selector(clickNameBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        self.nameView.hidden = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [bgView addGestureRecognizer:tap];
        
        self.sexView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW-60, 140)];
        self.sexView.backgroundColor = [UIColor whiteColor];
        self.sexView.center = CGPointMake(SCREENW/2, SCREENH/2);
        self.sexView.layer.cornerRadius = 8;
        self.sexView.layer.masksToBounds = YES;
        [self addSubview:self.sexView];
        
        w = self.sexView.frame.size.width/4;
        float spx =(self.sexView.frame.size.height- (self.sexView.frame.size.height/3)*2)/3;

        UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake((self.sexView.frame.size.width-w)/2, spx, w, self.sexView.frame.size.height/3)];
        [btn3 setTitle:@"男" forState:UIControlStateNormal];
        [btn3 setTitleColor:bgmmainColor forState:UIControlStateNormal];
        [btn3 setTitleColor:bgmmainColor forState:UIControlStateSelected];

        btn3.backgroundColor = bgmyelloeColor;
        btn3.layer.cornerRadius = 5;
        btn3.layer.masksToBounds = YES;
        btn3.tag = 102;
        [self.sexView addSubview:btn3];
        
        UIButton *btn4 = [[UIButton alloc]initWithFrame:CGRectMake(btn3.frame.origin.x, spx*2+btn3.frame.size.height, w, self.sexView.frame.size.height/3)];
        [btn4 setTitle:@"女" forState:UIControlStateNormal];
        [btn4 setTitleColor:bgmmainColor forState:UIControlStateNormal];
        [btn4 setTitleColor:bgmmainColor forState:UIControlStateSelected];
        btn4.backgroundColor = [UIColor whiteColor];
        btn4.layer.cornerRadius = 5;
        btn4.layer.masksToBounds = YES;
        [self.sexView addSubview:btn4];
        btn4.tag = 103;
        
        [btn3 addTarget:self action:@selector(clickNameBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btn4 addTarget:self action:@selector(clickNameBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        
        self.TitleView = [[UIView alloc]initWithFrame:CGRectMake(0, 60, SCREENW-60, SCREENH-120)];
        self.TitleView.backgroundColor = [UIColor whiteColor];
        self.TitleView.center = CGPointMake(SCREENW/2, SCREENH/2);
        self.TitleView.layer.cornerRadius = 8;
        self.TitleView.layer.masksToBounds = YES;
        [self addSubview:self.TitleView];
        
        self.userTitle = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 200, 50)];
        self.userTitle.font = [UIFont boldSystemFontOfSize:16];
        [self.TitleView addSubview:self.userTitle];
        
        self.userTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.TitleView.frame.size.width, self.TitleView.frame.size.height-100)];
        self.userTable.delegate = self;
        self.userTable.dataSource = self;
        self.userTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.TitleView addSubview:self.userTable];
        [self.TitleView setUserInteractionEnabled:YES];
        
        
        
        UIButton *btn5 = [[UIButton alloc]initWithFrame:CGRectMake(self.TitleView.frame.size.width-150, self.TitleView.frame.size.height-40, 60, 30)];
        [btn5 setTitle:@"确定" forState:UIControlStateNormal];
        [btn5 setTitleColor:bgmmainColor forState:UIControlStateNormal];
        btn5.backgroundColor = bgmyelloeColor;
        btn5.layer.cornerRadius = 5;
        btn5.layer.masksToBounds = YES;
        btn5.tag = 65;
        [btn5 addTarget:self action:@selector(clickTitleSele:) forControlEvents:UIControlEventTouchUpInside];
        btn5.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.TitleView addSubview:btn5];
        
        UIButton *btn6 = [[UIButton alloc]initWithFrame:CGRectMake(self.TitleView.frame.size.width-70, self.TitleView.frame.size.height-40,60, 30)];
        [btn6 setTitle:@"取消" forState:UIControlStateNormal];
        [btn6 setTitleColor:bgmmainColor forState:UIControlStateNormal];
        btn6.backgroundColor = [UIColor whiteColor];
        btn6.layer.cornerRadius = 5;
        btn6.layer.masksToBounds = YES;
        btn6.titleLabel.font = [UIFont systemFontOfSize:13];
        btn6.tag = 66;
        [btn6 addTarget:self action:@selector(clickTitleSele:) forControlEvents:UIControlEventTouchUpInside];

        [self.TitleView addSubview:btn6];
        
        
        
        self.dateView.hidden = YES;
        self.dateView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREENH-250, SCREENW, 250)];
        self.dateView.backgroundColor = bgmColor(244, 244, 244);
        [self addSubview:self.dateView];
        
        
        UIButton *btn9 = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-70, 0, 50, 50)];
        [btn9 setTitle:@"完成" forState:UIControlStateNormal];
        [btn9 setTitleColor:bgmmainColor forState:UIControlStateNormal];
        btn9.titleLabel.font = [UIFont systemFontOfSize:15];
        btn9.tag = 99;
        [self.dateView addSubview:btn9];
        
        UIButton *btn10 = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, 50, 50)];
        [btn10 setTitle:@"取消" forState:UIControlStateNormal];
        [btn10 setTitleColor:bgmmainColor forState:UIControlStateNormal];
        btn10.titleLabel.font = [UIFont systemFontOfSize:15];
        btn10.tag = 100;
        [self.dateView addSubview:btn10];
        [btn9 addTarget:self action:@selector(clickData:) forControlEvents:UIControlEventTouchUpInside];
        [btn10 addTarget:self action:@selector(clickData:) forControlEvents:UIControlEventTouchUpInside];

        
        self.datePicker = [ [ UIDatePicker alloc] initWithFrame:CGRectMake(0,50,SCREENW,200)];
        self.datePicker.backgroundColor = [UIColor whiteColor];
        self.datePicker.datePickerMode = UIDatePickerModeDate;
        self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
        [self.datePicker setMaximumDate: [NSDate new]];
//        [self.datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
        
        unsigned int outCount;
        int i;
        objc_property_t *pProperty = class_copyPropertyList([UIDatePicker class], &outCount);
        for (i = outCount -1; i >= 0; i--){
            // 循环获取属性的名字   property_getName函数返回一个属性的名称
            NSString *getPropertyName = [NSString stringWithCString:property_getName(pProperty[i]) encoding:NSUTF8StringEncoding];
            if([getPropertyName isEqualToString:@"textColor"]){
                [self.datePicker setValue:bgmyelloeColor forKey:@"textColor"];
            }
        }
        [self.dateView addSubview:self.datePicker];
        

    }
    return self;
}

-(void)clickData:(UIButton *)btn{
    int tag = (int)btn.tag;
    if (tag ==99) {
        NSDate *theDate = self.datePicker.date;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"YYYY-MM-dd";
        NSString *brith = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:theDate]];
        [self.delegate changeBirth:brith];

    }else{
        [self.delegate changeBirth:@""];
    }
    
}
//-(void)dateChange:(UIDatePicker *)datePicker{
//    NSDate *theDate = datePicker.date;
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    dateFormatter.dateFormat = @"YYYY-MM-dd";
////    self.birth = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:theDate]];
//    //    [self change];
//}

- (void)setUserSex:(BOOL) b{
    UIButton *nanbtn = [self.sexView viewWithTag:102];
    UIButton *nvbtn = [self.sexView viewWithTag:103];
    if (b) {
        nanbtn.selected = YES;
        nanbtn.backgroundColor = bgmyelloeColor;
        nvbtn.selected = NO;
        nvbtn.backgroundColor = [UIColor whiteColor];
    }else{
        nanbtn.selected = NO;
        nanbtn.backgroundColor = [UIColor whiteColor];
        nvbtn.selected = YES;
        nvbtn.backgroundColor = bgmyelloeColor;
    }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleContents.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"MeChangeTableViewCell";
    MeChangeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MeChangeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.changeCellDelegate = self;
    }
    NSDictionary *dic = [self.titleContents objectAtIndex:indexPath.row];
    if (self.isTitle) {  //行业
        cell.usertltleLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]];
        cell.seleBtn.hidden = YES;
        cell.usertltleLabel.frame = CGRectMake(20, 15, 300, 30);

    }else{  //个人标签
        cell.usertltleLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"lableName"]];
        cell.seleBtn.hidden = NO;
        cell.usertltleLabel.frame = CGRectMake(60, 15, 300, 30);
        cell.seleBtn.tag = (int)indexPath.row;
        NSString *sele = [self.userSeleArray objectAtIndex:indexPath.row];
        cell.seleBtn.selected = [sele isEqualToString:@"1"]?YES:NO;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [self.titleContents objectAtIndex:indexPath.row];
    if (self.isTitle) {  //行业
        NSString *str = [NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]];
        [self.delegate changeIndustry:str];
    }else{  //个人标签
         
        
    }
}
-(void)clikSeleBtn:(UIButton *)btn{
    int count = 0;
    for (NSString *key in self.userSeleArray) {
        if ([key isEqualToString:@"1"]) {
            count = count+1;
        }
    }
    if (count>=6) {
        if(!btn.selected){
            [SVProgressHUD showInfoWithStatus:@"只能选六个标签哦~"];
            return;
        }
    }
    int i = (int)btn.tag;
    btn.selected  = !btn.selected;
    
    if (btn.selected) {
        [self.userSeleArray replaceObjectAtIndex:i withObject:@"1"];
    }else{
        [self.userSeleArray replaceObjectAtIndex:i withObject:@"0"];
    }

}


- (void)tap{
    self.hidden = YES;
}
- (void)clickNameBtn:(UIButton *)btn{
    //100 取消 101确定
    int tag = (int)btn.tag;
    switch (tag) {
        case 100:
        {
            [self.delegate changeName:[BGMUser shareInstance].name];
            [self.nameText resignFirstResponder];
        }
            break;
        case 101:
        {
            if (self.nameText.text.length>0) {
                [self.delegate changeName:self.nameText.text];
            }else{
                NSLog(@"输入真实姓名");
            }
             [self.nameText resignFirstResponder];
        }
            break;
        case 102:  //男
        {
            [self.delegate changeSex:@"1"];
        }
            break;
        case 103: //女
        {
              [self.delegate changeSex:@"2"];
        }
            break;
            
        default:
            break;
    }
    
}

- (void)clickTitleSele:(UIButton *)btn{
    int tag = (int)btn.tag;
    if (tag == 65) {
        [self.delegate changeUserLabel:self.userSeleArray];
    }else if(tag == 66){
        [self.delegate hideUserLabel];
        
    }
}
@end
